package com.cskaoyan.service;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentExample;
import com.cskaoyan.bean.CommentListVO;
import com.cskaoyan.mapper.CommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;

    @Override
    public CommentListVO<Comment> queryCommentList(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {
        PageHelper.startPage(page,limit);
        CommentListVO<Comment> commentListVO = new CommentListVO<>();
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria =commentExample.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if(valueId!=null){
            criteria.andValueIdEqualTo(valueId);
        }
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        PageInfo pageInfo = new PageInfo(commentList);
        long total = pageInfo.getTotal();
        commentListVO.setItems(commentList);
        commentListVO.setTotal(total);
        return commentListVO;

    }

    @Override
    public int deleteComment(Comment comment) {
        int delete= commentMapper.deleteByPrimaryKey(comment.getId());
        return delete;
    }
}
